package org.example.admin.service;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.admin.dto.GoodsAllinone;
import org.example.common.utils.DateTimeUtil;
import org.example.db.entity.*;
import org.example.db.mapper.GoodsAttributeMapper;
import org.example.db.mapper.GoodsMapper;
import org.example.db.mapper.GoodsSpecificationMapper;
import org.example.db.mapper.OrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminGoodsService {
    GoodsMapper goodsMapper;
    GoodsAttributeMapper goodsAttributeMapper;
    GoodsSpecificationMapper goodsSpecificationMapper;
    OrderMapper orderMapper;

    public AdminGoodsService(GoodsMapper goodsMapper, GoodsAttributeMapper goodsAttributeMapper, GoodsSpecificationMapper goodsSpecificationMapper, OrderMapper orderMapper) {
        this.goodsMapper = goodsMapper;
        this.goodsAttributeMapper = goodsAttributeMapper;
        this.goodsSpecificationMapper = goodsSpecificationMapper;
        this.orderMapper = orderMapper;
    }

    public Goods getGoodsById(Long id){
        return goodsMapper.selectById(id);
    }

    public List<GoodsAttribute> getGoodsAttributes(Long goodsId){
        return goodsAttributeMapper.selectList(new QueryWrapper<GoodsAttribute>().lambda().eq(GoodsAttribute::getGoodsId,goodsId));
    }

    public List<GoodsSpecification> getGoodsSpecification(Long goodsId){
        return goodsSpecificationMapper.selectList(new QueryWrapper<GoodsSpecification>().lambda().eq(GoodsSpecification::getGoodsId,goodsId));
    }
    public GoodsSpecification getSpecificationById(Long spId){
        return goodsSpecificationMapper.selectById(spId);
    }

    public void updateGoodsAll(GoodsAllinone goodsAllinone){
        Goods goods = goodsAllinone.getGoods();
        List<GoodsAttribute> goodsAttributes = goodsAllinone.getGoodsAttributes();
        List<GoodsSpecification> goodsSpecifications = goodsAllinone.getGoodsSpecifications();
        String time = DateTimeUtil.getDateTimeString(LocalDateTime.now());
        goods.setAddTime(time);
        goods.setUpdateTime(time);
        goodsMapper.updateById(goods);
        for (GoodsAttribute goodsAttribute: goodsAttributes) {
            String time1 = DateTimeUtil.getDateTimeString(LocalDateTime.now());
            goodsAttribute.setAddTime(time1);
            goodsAttribute.setUpdateTime(time1);
            goodsAttributeMapper.updateById(goodsAttribute);
        }
        for (GoodsSpecification goodsSpecification:goodsSpecifications) {
            String time2 = DateTimeUtil.getDateTimeString(LocalDateTime.now());
            goodsSpecification.setAddTime(time2);
            goodsSpecification.setUpdateTime(time2);
            goodsSpecificationMapper.updateById(goodsSpecification);
        }

    }

    @Transactional
    public void insertGoodsAll(GoodsAllinone goodsAllinone){

        Goods goods = goodsAllinone.getGoods();
        List<GoodsAttribute> goodsAttributes = goodsAllinone.getGoodsAttributes();
        List<GoodsSpecification> goodsSpecifications = goodsAllinone.getGoodsSpecifications();
        String time = DateTimeUtil.getDateTimeString(LocalDateTime.now());
        goods.setAddTime(time);
        goods.setUpdateTime(time);
        goodsMapper.insert(goods);
        Long goodsId = goods.getId();
        for (GoodsAttribute goodsAttribute:goodsAttributes) {
            String time1 = DateTimeUtil.getDateTimeString(LocalDateTime.now());
            goodsAttribute.setAddTime(time1);
            goodsAttribute.setUpdateTime(time1);
            goodsAttribute.setGoodsId(goodsId);
            goodsAttributeMapper.insert(goodsAttribute);
        }
        for (GoodsSpecification goodsSpecification:goodsSpecifications) {
            String time2 = DateTimeUtil.getDateTimeString(LocalDateTime.now());
            goodsSpecification.setAddTime(time2);
            goodsSpecification.setUpdateTime(time2);
            goodsSpecification.setGoodsId(goodsId);
            goodsSpecificationMapper.insert(goodsSpecification);
        }
    }

    public List<GoodsOrder> getGoodsOrder(){
        return orderMapper.selectList(null);
    }

    public void updateOrderStatus(GoodsOrder goodsOrder){
        orderMapper.updateById(goodsOrder);
    }

    public void deleteOrder(Long id){
        orderMapper.deleteById(id);
    }


}
